package com.daily.pump.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.daily.pump.R;
import com.daily.pump.dao.DaoSharedPreferences;
import com.daily.pump.model.Device;
import com.xunxi.mimi.ui.base.BaseRecycleAdapter;
import com.xunxi.mimi.ui.base.RecycleViewHolder;
import com.xunxi.mimi.utils.LogUtil;

/**
 * Copyright (c) 2018 Miguan Inc All rights reserved.
 * Created by Liaopeikun on 2018/1/26
 * <p>
 * 可连接WiFi列表
 */
public class UserListAdapter extends BaseRecycleAdapter<Device> {

    private int selectP = -1;

    private WifiListAdapterListener mAdapterListener;

    public void setWifiListAdapterListener(WifiListAdapterListener listener) {
        this.mAdapterListener = listener;
    }

    public UserListAdapter(Context context) {
        super(context);
    }

    @Override
    public void getItemView(RecycleViewHolder holder, int position, Device scanResult) {
        TextView wifiTv = holder.getView(R.id.wifi_tv);
        ProgressBar progressBar = holder.getView(R.id.progress);
        wifiTv.setText(scanResult.deviceName);

        if (selectP == position) {
//            wifiTv.setSelected(true);
            progressBar.setVisibility(View.VISIBLE);
        } else {
//            wifiTv.setSelected(false);
            progressBar.setVisibility(View.GONE);
        }

        holder.getView(R.id.content_fl).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAdapterListener != null){
                    mAdapterListener.onItemClick(scanResult, position);
                }
            }
        });
        holder.getView(R.id.delete_fl).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDatas.remove(position);
                notifyRecyeItemRemoved(position);
                DaoSharedPreferences.getInstance().deleteDevice(scanResult);
            }
        });
        holder.getView(R.id.content_fl).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mAdapterListener != null){
                    mAdapterListener.onItemLongClick(scanResult, position);
                }
                return true;
            }
        });
    }

    @Override
    public int getItemResource(int viewType) {
        return R.layout.item_wifi_list;
    }

    public Device setSelect(int position) {
        LogUtil.error(UserListAdapter.class, "setSelect setSelect:"+position);
        if (selectP != position){
            selectP = position;
            notifyDataSetChanged();
        }
        return getItem(position);
    }

    public void clearSelect() {
        if (selectP != -1){
            selectP = -1;
            notifyDataSetChanged();
        }
    }

    public void addDevice(Device device) {
        removeSame(device);
        mDatas.add(0, device);
        notifyItemInserted(0);
    }

    private void removeSame(Device device) {
        for (int i = 0; i < mDatas.size(); i++) {
            Device deviceItem = mDatas.get(i);
            if (deviceItem.deviceName.equals(device.deviceName)){
                mDatas.remove(i);
                notifyItemRemoved(i);
                return;
            }
        }
    }

    public interface WifiListAdapterListener{
        void onItemClick(Device device, int position);
        void onItemLongClick(Device device, int position);
    }
}
